#pragma once
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#include <vector>
#include "nb_lock.h"

using std::vector;
using std::swap;

#define BUFFER_SIZE 4096


typedef enum
{
	OP_RECV,
	OP_SEND,
	OP_CLOSE

}OPERATION;

struct NB_Msg
{
	OVERLAPPED	overlapped;
	WSABUF		wsabuf;
	char		buffer[BUFFER_SIZE];
	DWORD		dwBytesOfRecvd;
	DWORD		dwFlags;
	OPERATION	operation;
	SOCKET		socket;
	SOCKADDR_IN	addr;
	size_t		pos;

	NB_Msg();
	void Reset();
};

class NB_MsgManager
{
public:
	NB_MsgManager(int nMaxSize);
	~NB_MsgManager();

public:
	NB_Msg * Create();
	void Free(NB_Msg * lpMsg);

private:
	int  m_nMaxSize;

	NB_Msg *		m_pMsgArray;
	vector<NB_Msg*>	m_FreeList;
	vector<NB_Msg*>	m_UsedList;

	CsLock * m_pCsLock;
};